package com.gcit.seniorWish.web.config;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.gcit.seniorWish.web.common.persistence.model.Dict;
import com.gcit.seniorWish.web.config.web.MsgProducer;
import com.gcit.seniorWish.web.modular.system.service.IDictService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;


@Service
public class TestSchedule implements ApplicationContextAware {
    Logger logger = LoggerFactory.getLogger(TestSchedule.class);

    private  ApplicationContext applicationContext;
//    @Resource
//    private IDictService dictService;
 
//    @Scheduled(fixedDelay = 1000)  //定时任务1
//    public void printXXXXXXX(){
//        try{
//            Thread.sleep(5000);  //睡眠5秒
//            logger.info("定时任务1"+Thread.currentThread().getName() +"-----" +
//                    "--"+ LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH;ss:mm"))); //打印当前线程名字
//
//        }catch (Exception e){
//            logger.error(e.getMessage());
//        }
//    }
//
//    @Scheduled(fixedDelay = 1000)  //定时任务2
//    public void printYYYYYYY(){
//        try{
//            Thread.sleep(5000);
//            logger.info("定时任务2"+Thread.currentThread().getName() +"-----" +
//                    "--"+ LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH;ss:mm")));
//
//        }catch (Exception e){
//            logger.error(e.getMessage());
//        }
//    }
    @Scheduled(cron = "0/15 * * * * ?")  //定时任务2
    public void printZZZZZZZZZZZZ(){
        try{
            Thread.sleep(5000);
            logger.info("定时任务3"+Thread.currentThread().getName() +"-----" +
                    "--"+ LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH;ss:mm")));
            //cleardata();
            MsgProducer msgProducer= (MsgProducer) applicationContext.getBean("msgProducer");
            msgProducer.sendMsg("定时任务3"+Thread.currentThread().getName() +"-----" +
                    "--"+ LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH;ss:mm")));
        }catch (Exception e){
            logger.error(e.getMessage());
        }
    }
    private  void cleardata(){
        IDictService dictService= (IDictService) applicationContext.getBean("dictService");
        EntityWrapper<Dict> ew = new EntityWrapper<>();

        //只查询根字典
        ew.eq("pid","0");

        List<Dict> records = dictService.selectList(ew) ;
        for (Dict map : records){
            System.out.println("11111111111111");
            System.out.println(map.getCode().toString()+"--"+map.getName().toString());
        }
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
            this.applicationContext = applicationContext ;
    }
    public ApplicationContext getApplicationContext() throws BeansException {
        return applicationContext ;
    }
}
